package com.atguigu.gmall.mq.receiver;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-20 16:19
 **/
@Component
public class DelayReceiver {

    /*@RabbitListener(queues = DelayedMqConfig.queue_delay_1)
    public void get(String msg, Message message, Channel channel) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Receive queue_delay_1: " + sdf.format(new Date()) + " Delay rece." + msg);

        // false 确认一个消息，true 批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }*/

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue.delay.1", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = "exchange.delay",arguments = {
                    @Argument(name = "x-delayed-type",value ="direct")
            },type = "x-delayed-message"),
            key = {"routing.delay"}
    ))
    public void get(String msg, Message message, Channel channel) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Receive queue_delay_1: " + sdf.format(new Date()) + " Delay rece." + msg);

        // false 确认一个消息，true 批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
